<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\service\Redis;
use think\Db;
use addons\third\library\Service;
use addons\third\model\Third;
use app\common\library\Auth;
use fast\Http;
use think\Config;

use think\Validate;

use think\Log;
use app\api\controller\Sms;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $IP = $_SERVER["REMOTE_ADDR"];
        $url = "http://api.guomhy.com/api/v1/getuseriparea?ip=" . $IP;
        $IP_code = file_get_contents($url);
        $data['mch_id'] = $IP_code . $this->randomkeys(12);
        $this->success('请求成功', $data);
    }


    /*生成随机数*/
    public function randomkeys($length)
    {
        $pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
        $key = '';
        for ($i = 0; $i < $length; $i++) {
            $key .= $pattern{mt_rand(0, 35)};    //生成php随机数
        }
        return $key;
    }

    public function arr()
    {
        $userInfo = [
            'nickname' => '凯',
            'gender' => 1,
            'avatar' => 'xxx',
        ];
        $platform = 'wxapp';
        $json['expires_in'] = 7200;
        $result = [
            'mobile' => '18601069658',
            'onlinesume' => [
//                'company' => 'thinkphp',
//                'title' => '项目名称',
//                'content' => '项目介绍',
//                'description' => '我的贡献',
//                'email' => 'thinkphp@qq.com'

                ['company' => 'thinkphp', 'title' => 'thinkphp@qq.com', 'content' => '项目介绍', 'description' => '我的贡献', 'email' => 'thinkphp@qq.com'],

            ],
            'skill' => 'skill,xxx2,xxx3',
            'expertise' => 'php',
            'notesfile' => '/uploads/20200205/834e8fb221e86147e686a903606229f8.pdf',
            'card_num' => '110224178903254012',
            'card_endtime' => '1583563399',
            'card_zheng' => '/uploads/20200205/834e8fb221e86147e686a903606229f8.png',
            'card_fan' => '/uploads/20200205/834e8fb221e86147e686a903606229f8.png',
            'username' => '孙凯',
            'openid' => 'openid',
            'userinfo' => [
                'nickname' => $userInfo['nickname'],
            ],
            'access_token' => 'session_key',
            'refresh_token' => '',
            'expires_in' => isset($json['expires_in']) ? $json['expires_in'] : 0,
        ];
        $extend = ['gender' => $userInfo['gender'], 'nickname' => $userInfo['nickname'], 'avatar' => $userInfo['avatar']];
//                Log::error($extend);//到这里了是空的
        $ret = Service::connect($platform, $result, $extend);
        //Log::error($ret);
        if ($ret) {
            $auth = Auth::instance();
//                    Log::error($auth);
            $this->success("登录成功", ['userInfo' => $auth->getUserinfo()]);
        } else {
            $this->error("连接失败");

        }
    }

    public function test()
    {
        /*Redis::getInstance()->set('test', 'guo', 100);
        dump(Redis::getInstance()->get('test'));
        dump(\config('redis'));*/
        exit;
    }

}
